{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参数实验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/user/miniconda3/envs/yjq-ml/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('../common/')\n",
    "from evaluator import *\n",
    "from utils import *\n",
    "import warnings\n",
    "import os\n",
    "import numpy as np\n",
    "import torch\n",
    "import sys\n",
    "from usad_model import  *\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.data.sampler import SubsetRandomSampler\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_datase(dataset):\n",
    "    folder = os.path.join(\"../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = loader[2][:,0].reshape(-1,1)\n",
    "    return train_data, test_data, labels\n",
    "def convert_to_windows(data, window_size):\n",
    "    windows = []\n",
    "    length  = data.shape[0]\n",
    "    for i in range(length):\n",
    "        if length - i >= window_size:\n",
    "            window = data[i:i+window_size]\n",
    "        else:\n",
    "            window = data[i-window_size+1:i+1]\n",
    "        windows.append(window)    \n",
    "    return np.array(windows)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 不同窗口大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "window_sizes = [5, 10, 20, 50, 100]\n",
    "train_data, test_data, labels = load_datase(\"SWaT\")\n",
    "batch_size = 1024\n",
    "num_epochs = 70\n",
    "z_dim = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [15:58<00:00, 13.69s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7648435444782923, 'precision': 0.9933029183502067, 'recall': 0.6218304314051682, 'TP': 33965.0, 'TN': 395069.0, 'FP': 229.0, 'FN': 20656.0, 'threshold': 19.640852218627657}\n",
      "{'f1-score': 0.8429273166476093, 'precision': 0.9026924200629849, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390643.0, 'FP': 4655.0, 'FN': 11438.0, 'threshold': 9.764685356140083}\n",
      "10\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [19:18<00:00, 16.55s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7648660631969714, 'precision': 0.9933321632469636, 'recall': 0.6218487393819504, 'TP': 33966.0, 'TN': 395070.0, 'FP': 228.0, 'FN': 20655.0, 'threshold': 19.55950579833982}\n",
      "{'f1-score': 0.842960226042, 'precision': 0.9027679054850591, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390647.0, 'FP': 4651.0, 'FN': 11438.0, 'threshold': 9.97073258209228}\n",
      "20\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [16:09<00:00, 13.86s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.765068731665082, 'precision': 0.9935953673177763, 'recall': 0.6220135111729895, 'TP': 33975.0, 'TN': 395079.0, 'FP': 219.0, 'FN': 20646.0, 'threshold': 19.45839697265607}\n",
      "{'f1-score': 0.829034329771968, 'precision': 0.8714155986537351, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 388926.0, 'FP': 6372.0, 'FN': 11438.0, 'threshold': 9.576127243041961}\n",
      "50\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [19:01<00:00, 16.31s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7674853079703085, 'precision': 0.988771504159805, 'recall': 0.627139744671987, 'TP': 34255.0, 'TN': 394909.0, 'FP': 389.0, 'FN': 20366.0, 'threshold': 13.54797304915902}\n",
      "{'f1-score': 0.8259475435700588, 'precision': 0.9900483509452662, 'recall': 0.7085187014685709, 'TP': 38700.0, 'TN': 394909.0, 'FP': 389.0, 'FN': 15921.0, 'threshold': 13.54797304915902}\n",
      "100\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [36:42<00:00, 31.46s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7719731668397907, 'precision': 0.9867498714917792, 'recall': 0.633986927988505, 'TP': 34629.0, 'TN': 394833.0, 'FP': 465.0, 'FN': 19992.0, 'threshold': 13.045212326049608}\n",
      "{'f1-score': 0.830357246552734, 'precision': 0.9788238800569607, 'recall': 0.721004741633986, 'TP': 39382.0, 'TN': 394446.0, 'FP': 852.0, 'FN': 15239.0, 'threshold': 12.550000892638906}\n"
     ]
    }
   ],
   "source": [
    "results1 = {}\n",
    "results2 = {}\n",
    "for window_size in window_sizes:\n",
    "    print(window_size)\n",
    "    # 转换为时间窗口\n",
    "    train_window = convert_to_windows(train_data, window_size)\n",
    "    test_window = convert_to_windows(test_data, window_size)\n",
    "    train_dataset = torch.from_numpy(train_window).float().view([train_window.shape[0], train_window.shape[1]*train_window.shape[2]])\n",
    "    test_dataset =  torch.from_numpy(test_window).float().view([test_window.shape[0], test_window.shape[1]*test_window.shape[2]])\n",
    "    train_gaussian_percentage  = 0.2\n",
    "    indices = np.random.permutation(len(train_dataset))\n",
    "    split_point = int(train_gaussian_percentage * len(train_dataset))\n",
    "    train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, drop_last=True,\n",
    "                                sampler=SubsetRandomSampler(indices[:-split_point]), pin_memory=False)\n",
    "    val_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, drop_last=True,\n",
    "                            sampler=SubsetRandomSampler(indices[-split_point:]),pin_memory=False)\n",
    "    test_loader = DataLoader(dataset = test_dataset, batch_size=batch_size, shuffle=False, drop_last=False)\n",
    "\n",
    "    model = UsadModel(train_data[0].shape[0]*window_size, z_dim)\n",
    "    history = model.fit(\n",
    "        train_loader=train_loader,\n",
    "        validation_loader=val_loader,\n",
    "        epochs=num_epochs\n",
    "    )\n",
    "    pred= model.predict_prob(test_loader)\n",
    "    result1 = bf_search(labels.squeeze(), pred, verbose=False, is_adjust=False) \n",
    "    print(result1)\n",
    "    result2 = bf_search(labels.squeeze(), pred, verbose=False, is_adjust=True)\n",
    "    print(result2)\n",
    "    results1[window_size] = result1\n",
    "    results2[window_size] = result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7648435444782923, 0.7648660631969714, 0.765068731665082, 0.7674853079703085, 0.7719731668397907]\n",
      "[0.9933029183502067, 0.9933321632469636, 0.9935953673177763, 0.988771504159805, 0.9867498714917792]\n",
      "[0.6218304314051682, 0.6218487393819504, 0.6220135111729895, 0.627139744671987, 0.633986927988505]\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results1.values())]\n",
    "precisions = [item['precision'] for item in list(results1.values())]\n",
    "recalls = [item['recall'] for item in list(results1.values())]\n",
    "print(f1_scores)\n",
    "print(precisions)\n",
    "print(recalls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.8429273166476093, 0.842960226042, 0.829034329771968, 0.8259475435700588, 0.830357246552734]\n",
      "[0.9026924200629849, 0.9027679054850591, 0.8714155986537351, 0.9900483509452662, 0.9788238800569607]\n",
      "[0.7905933613828758, 0.7905933613828758, 0.7905933613828758, 0.7085187014685709, 0.721004741633986]\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results2.values())]\n",
    "precisions = [item['precision'] for item in list(results2.values())]\n",
    "recalls = [item['recall'] for item in list(results2.values())]\n",
    "print(f1_scores)\n",
    "print(precisions)\n",
    "print(recalls)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
